package com.yupi.project.config;

import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor getExecutor() {
        ThreadFactory threadFactory = new ThreadFactory() {
            private int count = 0;
            @Override
            public Thread newThread(@NotNull Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("当前线程:"+count);
                count++;
                return thread;
            }
        };

        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(2,4,100000, TimeUnit.MICROSECONDS,new ArrayBlockingQueue<>(4),threadFactory,new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}
